<?php //pack11_class

class SettingsManager {
	
	private static $settingsByName=array();
    private static $settingsByID=array();
    private static $settingsDone = false;

    public static function settings() {
        return (count(self::$settingsByID)>0)? self::$settingsByID:self::setSettings(true);
    }

    public static function idMap() {
        return (count(self::$settingsByName)>0)? self::$settingsByName:self::setSettings();
    }

    public static function getSetting($id) {
        
        array_push(Tracer::$traceFrom,__CLASS__);

        //echo "getSetting started whith id: ".id;
        if(!self::$settingsDone) self::setSettings();

        //trace(self::$settingsByID);
        
        if(is_numeric($id)) {
            if(array_key_exists($id, self::$settingsByID)){
                return self::$settingsByID[$id];
            } else {
                //trace("Setting {$id} not found!");
                return false;
            }
        } else {
            if(array_key_exists($id,$_SESSION)) {
                if(array_key_exists("state",$_GET)) {
                    if($_GET["state"]!=MD5("start")) self::$settingsByName[$id]->setValue($_SESSION[$id]);
                }
            }
            if(array_key_exists($id, self::$settingsByName)){
                return self::$settingsByName[$id];
            } else {
                //trace("Setting {$id} not found!");
                return false;
            }
        }

        return false;
    }

	private static function setSettings($byID=false) {

        $_settings=MySQL::TABLE_SETTINGS;

		$sql=MySQLGet::process("Select * from {$_settings};");
			
		self::$settingsByName=array();
                self::$settingsByID=array();

	foreach($sql as $name=>$setting) {
            trace(($setting["system"]=="userin" && array_key_exists("exit", $_GET))? "true":"false");
            if(!($setting["system"]=="userin" && array_key_exists("exit", $_GET))) {
		$item=new Setting;

                $item->id=$setting["id"];
                $item->system=$setting["system"];
                $item->sessionUse=($setting["session"]==0)? false:true;
                if($setting["value"]) $item->value=$setting["value"];
                //trace($item->system." ".$item->value);
                if(array_key_exists($item->system,$_SESSION)) {
                    if(array_key_exists($id,$_SESSION)) {
                        if($_GET["state"]!=MD5("start")) $item->value=$_SESSION[$item->system];
                    }
                }
                //trace($item->system." ".$item->value);

		self::$settingsByName[$item->system]=$item;
                self::$settingsByID[$item->id]=$item;
            }
	}

            self::$settingsDone=true;

            return ($byID)? self::$settingsByID:self::$settingsByName;
	}

        public static function setSetting($id,$value) {
            $setting=self::getSetting($id);
            $setting->setValue($value);
            //if($setting->system=="currency") trace("ALARMAAAA!!!! ".$value." ".$setting->value);
        }

        public static function traceAllSettings() {
            $str="";
            foreach(self::settings() as $setting) {
                if(is_numeric($setting->value))$str.=$setting->system.":".$setting->value."; ";
            }
            trace($str);
        }
}
?>